<template>
	<view>
		<view class="body after-navber">
			<view style="display: flex; flex-direction: column; height: 100%; width: 100%">
				<view style="flex-grow: 0">
					<form>
						<view class="search-box">
							<view class="input-box">
								<view style="margin: 0 20rpx;">
									<input :autoFocus="true" @blur="inputBlur" @confirm="inputConfirm"
										@focus="inputFocus" class="search-input" confirmType="search"
										style="width: 100%" />
								</view>
							</view>
							<view>
								<view @tap="searchCancel" class="search-cancel">取消</view>
							</view>
						</view>
					</form>
				</view>
				<view style="flex-grow: 3; position: relative">
					<view class="search-history" v-if="show_history && recordlist && recordlist.length > 0">
						<view>
							<text class="search-history-title">搜索历史</text>
		 				<view @tap="clearWords" class="delete-search-history">
								<image src="/static/images/icon-delete.png" style="width: 28rpx; height: 34rpx"></image>
							</view>
						</view>
						<view class="search-history-list">
							<view @tap="historyClick" class="search-history-item" :data-value="item"
								v-for="(item, index) in recordlist" >
								{{ item }}
							</view>
						</view>
					</view>
					<block v-if="show_result">
						<view class="search-result" v-if="is_search">
							<navigator :url="'/pages2/group/merchant/merchant?id='+item.id" class="sjlist flex" v-for="(item,index) in shop_list" :key="item">
								<image :src="item.image" class="flex-grow-0 img" mode="aspectFill"></image>
								<view class="flex-grow-1">
									<view class="flex-y-center">
										<view class="name flex-grow-1">{{item.name}}</view>
										<view class="flex-grow-0">售{{item.sales_num}}</view>
									</view>
									<view class="flex-y-center">
										<image src="/static/images/shop5_active.png" class="icon"
											v-for="(item4, index4) in item.star - 0" :key="index"></image>
										<image src="/static/images/shop5.png" class="icon" v-for="(item4, index4) in 5 - item.star"
											:key="index"></image>
										<!-- <view class="score">{{item.score}}</view> -->
										<view class="flex-grow-1 gray-text">
											￥{{item.avg_money}}/人
										</view>
										
										<view class="gray-text">{{item.distance}}</view>
									
									</view>
									<view class="text2">
										<text class="item">{{item.cate.name}}</text>
										<text class="item">{{item.addr}}</text>
									</view>
									<view class="label flex">
									<!-- 	<view v-for="(item2,index2) in item.label" class="item">{{item2}}</view> -->
									</view>
									<view class="line"></view>
									<view v-for="(item3,index3) in item.storevoucher" class="flex-y-center menu">		
										<view class="flex-grow-1 flex-y-center">
											<view class="hui flex-grow-0">惠</view>
											<view class="price">￥{{item3.voucher.money}}</view>
										</view>
										<view class="zhe flex-grow-0"><text
												style="margin-right: 15rpx;background:#fb6018;color: #fff;">赠送</text>{{item3.voucher.score}}{{money_name}}
										</view>
									<!-- <view class="yuanjia">赠送{{item3.voucher.score}}{{money_name}}</view>
										<view class="name2">{{item3.name}}</view> -->
									</view>
								</view>
							</navigator>
	
						</view>
						<view class="search-result" v-else>
							<view class="flex-row flex-x-center" style="padding: 36rpx 0">
								<view class="flex-row flex-y-center">
									<view class="flex-grow-0"
										style="width: 100rpx; height: 100rpx; margin-right: 40rpx">
										<image src="/static/images/Cn3VLepkZwbG0iN9.png"
											style="width: 100rpx; height: 100rpx"></image>
									</view>
									<view class="flex-grow-1" style="color: #353535">抱歉，没有相关商品</view>
								</view>
							</view>
						</view>
					</block>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {Index} from '../../../apirequest/index-model.js';
	var i = new Index();
	export default {
		data() {
			return {
				show_history: true,
				show_result:false,
				is_search:false,
				recordlist: [],
				shop_list: [],
				has_more:true,
				page:1,
				lat:0,
				lng:0,
				money_name:this.globalData.money_name,
			}
		},
		onLoad(options) {
			this.recordlist = uni.getStorageSync('recordlist') ? uni.getStorageSync('recordlist') : [],
				this.getUserLocation();
		},
		onReachBottom() {
			this._getList();
		},
		methods: {
			inputFocus: function() {
				this.show_history=true
				this.show_result=false
			},
			//清除历史搜索记录
			clearWords(){
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确认清空历史搜索记录吗?',
					success: function (res) {
						if (res.confirm) {
							uni.setStorageSync('recordlist',[])
							that.recordlist = [];
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			inputBlur: function() {
				var that = this;

				if (0 < that.shop_list.length) {
					setTimeout(function() {
						this.show_history=false
						this.show_result=true
					}, 300);
				}
			},

			inputConfirm: function(t) {
				var that = this;
				var e = t.detail.value;
				if (0 != e.length) {
					this.keyword = e
					that.shop_list= [];
					that.has_more=true;
					that.page=1
					that.getGoodsList();
				}
			},

			searchCancel: function() {
				uni.navigateBack({
					delta: 1
				});
			},

			historyClick: function(t) {
				var a = t.currentTarget.dataset.value;

				if (0 != a.length) {
					this.keyword = a
					this.shop_list= [];
					this.has_more=true;
					this.page=1
					this.getGoodsList();
				}
			},
			getGoodsList: function() {
				var that = this;
				var keyword = this.keyword;
				if(!keyword || keyword == '' || keyword == null){
					uni.showToast({
						title:'请输入搜索内容',
						icon:'none'
					})
					return false;
				}
				var recordlist = uni.getStorageSync('recordlist') ? uni.getStorageSync('recordlist') : [];
				if(recordlist.indexOf(keyword) !== -1){
					recordlist.splice(recordlist.indexOf(keyword), 1);
				}
				recordlist = [keyword].concat(recordlist);
				if(recordlist.length > 10){
					recordlist.splice(10,1);
				}
				uni.setStorageSync('recordlist',recordlist)
				this.recordlist = recordlist;
				var page = this.page;
			
				if(!this.has_more){
					return;
				}
				i.get_store_list({keyword:keyword,page:page,lat:this.lat,lng:this.lng},(res) => {
					if(res.code == 1){
						var shop_list = that.shop_list;
						shop_list = shop_list.concat(res.data.data)
						that.shop_list = shop_list;
						that.page = page+1
						that.has_more=res.data.has_more
						this.show_history = false
						this.show_result = true
					
						if(that.shop_list.length > 0){
							this.is_search = true
						}
					}
				})
			

			},
			//位置
				  getUserLocation: function () {
						let vm = this;
						uni.getSetting({
						  success: (res) => {
			
							// res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
							// res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
							// res.authSetting['scope.userLocation'] == true    表示 地理位置授权
							if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
							  uni.showModal({
								title: '请求授权当前位置',
								content: '需要获取您的地理位置，请确认授权',
								success: function (res) {
								  if (res.cancel) {
									uni.showToast({
									  title: '拒绝授权',
									  icon: 'none',
									  duration: 1000
									})
									
								  } else if (res.confirm) {
									uni.openSetting({
									  success: function (dataAu) {
										if (dataAu.authSetting["scope.userLocation"] == true) {
										  uni.showToast({
											title: '授权成功',
											icon: 'success',
											duration: 1000
										  })
										  //再次授权，调用wx.getLocation的API
										  vm.getLocation();
										} else {
										  uni.showToast({
											title: '授权失败',
											icon: 'none',
											duration: 1000
										  })
										
										}
									  }
									})
								  }
								}
							  })
							} else if (res.authSetting['scope.userLocation'] == undefined) {
							  //调用wx.getLocation的API
							  vm.getLocation();
							} else {
							  //调用wx.getLocation的API
							  vm.getLocation();
							}
						  }
						})
					  },
					  // 微信获得经纬度
					  getLocation: function () {
						let vm = this;
						uni.getLocation({
						  type: 'wgs84',
						  success: function (res) {
			
							var latitude = res.latitude
							var longitude = res.longitude
							vm.lat = latitude;
							vm.lng = longitude;
							
			
						  },
						  fail: function (res) {
						
						
						  }
						})
					  },
		
		}
	}
</script>

<style>
	form {
		padding: 16rpx 24rpx;
		display: block;
	}

	.search-box {
		display: flex;
		flex-direction: row;
	}

	.search-box view {
		flex-grow: 1;
		align-items: center;
	}

	.search-box view:last-child {
		flex-grow: 0;
	}

	.cat {
		font-size: 11pt;
		margin: 0 10rpx 0 24rpx;
	}

	.pull-down {
		width: 124rpx;
	}

	.pull-down-list {
		background-color: #fff;
		position: absolute;
		top: 80rpx;
		width: 150rpx;
		border: 1px solid #e3e3e3;
		border-radius: 15rpx;
		z-index: 999;
	}

	.pull-down-text {
		font-size: 11pt;
		height: 88rpx;
		border-bottom: 1px solid #e3e3e3;
	}

	.active {
		color: #ff5c5c;
	}

	.input-box {
		display: flex;
		flex-direction: row;
		background: #fff;
		border: 1rpx solid #e3e3e3;
		height: 64rpx;
		border-radius: 10rpx;
	}

	.input-box view {
		flex-grow: 1 !important;
		align-items: center;
	}

	.input-box view:first-child {
		flex-grow: 0;
	}

	.search-box .search-icon {
		height: 24rpx;
		width: 24rpx;
	}

	.search-box .search-cancel {
		display: inline-block;
		height: 64rpx;
		line-height: 68rpx;
		color: #00c203;
		white-space: nowrap;
		padding: 0 24rpx;
		margin-right: -24rpx;
	}

	.search-history {
		padding: 24rpx 34rpx;
	}

	.search-history .search-history-title {
		color: #666666;
	}

	.search-history .delete-search-history {
		float: right;
		padding: 15rpx 20rpx;
		margin-top: -15rpx;
	}

	.search-history-list {
		padding: 24rpx 0 0 0;
	}

	.search-history-list .search-history-item {
		display: inline-block;
		height: 64rpx;
		line-height: 64rpx;
		padding: 0 32rpx;
		margin: 0 20rpx 20rpx 0;
		background: #ddd;
		border-radius: 10rpx;
	}

	.loading-bar {
		text-align: center;
		padding: 0;
		visibility: hidden;
		height: 0;
	}

	.loading-bar.active {
		visibility: visible;
		height: auto;
		padding: 20rpx;
	}

	.goods-list {
		margin-left: -10rpx;
		margin-right: -10rpx;
	}

	.goods-item {
		width: 365rpx;
		display: inline-block;
		position: relative;
		margin: 0 10rpx;
		margin-bottom: 20rpx;
		font-size: 0;
		background: #fff;
	}

	.goods-item .goods-pic {
		width: 100%;
		height: 365rpx;
	}

	.goods-item .goods-info {
		padding: 5rpx 0;
	}

	.goods-item .goods-name {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		padding: 10rpx;
		font-size: 9pt;
		display: block;
		text-align: center;
	}

	.goods-item .goods-price {
		font-size: 11pt;
		color: #f40;
		display: block;
		text-align: center;
	}

	.loading-more-bar {
		text-align: center;
		margin-bottom: 20rpx;
		opacity: 0;
	}

	.loading-more-bar.active {
		opacity: 1;
	}
	.sjlist {
			border-radius: 15rpx;
			margin-bottom: 25rpx;
			padding: 20rpx;
			background: #fff;
			font-size: 0.75rem;
		}
	
		.sjlist .img {
			width: 160rpx;
			height: 160rpx;
			border-radius: 10rpx;
			margin-right: 10rpx;
		}
	
		.sjlist .name {
			font-size: 0.95rem;
			font-weight: 700;
			margin-bottom: 6rpx;
		}
	
		.sjlist .icon {
			width: 20rpx;
			height: 20rpx;
			margin-right: 6rpx;
		}
	
		.sjlist .score {
			color: #CC3433;
			font-size: 0.85rem;
			font-weight: 700;
		}
	
		.sjlist .gray-text {
			color: #8b8b8b;
			font-size: 0.85rem;
			margin-left: 10rpx;
		}
	.sjlist .text2{
		font-size: 0.85rem;
		color: #8b8b8b;
		margin: 15rpx 0;
	}
	.sjlist .text2 .item{
			margin-right: 10rpx;
	}
		.sjlist .label .item {
			background: #f4f4f4;
			border-radius: 10rpx;
			padding: 0 10rpx;
			margin-right: 10rpx;
			color: #8b8b8b;
		}
	
		.sjlist .line {
			width: 100%;
			height: 2rpx;
			background: #f1f1f1;
			margin: 15rpx 0;
		}
	
		.sjlist .menu {
			margin-bottom: 10rpx;
			
		}
	
		.sjlist .menu .hui {
			background: linear-gradient(to right, #fb6018, #f88309);
			color: #fff;
			border-radius: 12rpx;
			width: 38rpx;
			height: 38rpx;
			text-align: center;
			line-height: 38rpx;
			margin-right: 0rpx;
		}
	
		.sjlist .menu .price {
			color: #fb6018;
			font-weight: 700;
			font-size: 1rem;
		}
	
		.sjlist .menu .zhe {
			color: #fb6018;
			border: 1px solid #fb6018;
			border-radius: 10rpx;
			padding: 0 10rpx 0 0;
			margin: 0 10rpx;
		}
	
		.sjlist .menu .yuanjia {
			text-decoration: line-through;
			color: #bdbbbc;
			font-size: 0.95rem;
		}
	
		.sjlist .menu .name2 {
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
			margin-left: 20rpx;
			width: 178rpx;
		}
</style>
